package com.zhan.ielts.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.List;

import com.zhan.frame.core.utils.FileUtils;


public class HTMLSpirit {
    public static String getHTML(String pageURL, String encoding) {
        StringBuilder pageHTML = new StringBuilder();
        try {
            URL url = new URL(pageURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-Agent", "MSIE 7.0");
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
            String line = null;
            while ((line = br.readLine()) != null) {
                pageHTML.append(line);
                pageHTML.append("\r\n");
            }
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageHTML.toString();
    }
    
    
    public static void main(String args[]) throws ClassNotFoundException, SQLException{
        System.out.println("--------start");
    	String root = "http://www.iciba.com/";
    	String folder = "/usr/local/backup/html/";
//        System.out.println(getHTML("http://www.iciba.com/static", "utf-8"));
    	if(args.length<2){
    		return;
    	}
//    	String content = FileUtils.readFile("/usr/local/backup/"+"1.txt");
//    	System.out.println("--"+content.toString());
    	
    	 File file = new File("/usr/local/backup/"+"1.txt");
         if (file.exists()) { // 文件存在时
             try {
                 InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8");
                 BufferedReader bufferedReader = new BufferedReader(read);
                 StringBuilder buffer = new StringBuilder();
                 String lineTxt = null;
                 int i=0;
                 while((lineTxt = bufferedReader.readLine()) != null){
//                     buffer.append(lineTxt);
					String html = getHTML(root + lineTxt, "utf-8");
					FileUtils.createFile(folder+lineTxt+".html",html);
					System.out.println("生成了 "+i+" 个文件"+"-----文件"+ lineTxt+".html");
					i++;
                 }
                 read.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
         
//    	List<String> list = DictionaryUtils.getWordFromDictionary(52785, 100000);
        
//        for(int i=0;i<list.size();i++){
//        	String word = list.get(i).trim();
//        	String html = getHTML(root+word, "utf-8");
//        	FileUtils.createFile(folder+word+".html",html);
//            System.out.println("生成了 "+i+" 个文件"+"-----文件"+ word+".html");
//        }
        System.out.println("--------end");
        
        
    }
    
    public static void main2(String args[]) throws ClassNotFoundException, SQLException{
        System.out.println("--------start");
    	String root = "http://www.iciba.com/";
    	String folder = "/usr/local/backup/html/";
//        System.out.println(getHTML("http://www.iciba.com/static", "utf-8"));
    	if(args.length<2){
    		return;
    	}
    	int from = new Integer(args[0]);
    	int to = new Integer(args[1]);
    	
//        List<String> list = DictionaryUtils.getWordFromDictionary(from, to);
//    	List<String> list = DictionaryUtils.getWordFromDictionary(23121, 100000);
//    	List<String> list = DictionaryUtils.getWordFromDictionary(42320, 100000);
    	List<String> list = DictionaryUtils.getWordFromDictionary(52785, 100000);
        
//        List<String> list = DictionaryUtils.getWordFromDictionary(50000, 100000);
        for(int i=0;i<list.size();i++){
        	String word = list.get(i).trim();
        	String html = getHTML(root+word, "utf-8");
        	FileUtils.createFile(folder+word+".html",html);
            System.out.println("生成了 "+i+" 个文件"+"-----文件"+ word+".html");
        }
        System.out.println("--------end");
        
        
    }
} 